<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="false"
      label-width="68px"
    >
      <el-form-item label="函数名" prop="functionName">
        <el-select
          v-model="queryParams.functionName"
          placeholder="请输入函数名"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_sql_select_func"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关键字名" prop="keywordName">
        <el-input
          v-model="queryParams.keywordName"
          placeholder="请输入关键字名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表名" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段名" prop="fieldName">
        <el-input
          v-model="queryParams.fieldName"
          placeholder="请输入字段名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作符" prop="operator">
        <el-input
          v-model="queryParams.operator"
          placeholder="请输入操作符"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="比较值" prop="compareValue">
        <el-input
          v-model="queryParams.compareValue"
          placeholder="请输入比较值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查询语句id" prop="sqlId">
        <el-input
          v-model="queryParams.sqlId"
          placeholder="请输入查询语句id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['conf:BiSqlWhereDwd:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['conf:BiSqlWhereDwd:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['conf:BiSqlWhereDwd:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['conf:BiSqlWhereDwd:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="BiSqlWhereDwdList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <!-- <el-table-column label="函数名" align="center" prop="functionName" /> -->
      <!-- <el-table-column label="关键字名" align="center" prop="keywordName" /> -->
      <!-- <el-table-column label="优先级" align="center" prop="priorityLevel" />

      <el-table-column label="且或" align="center" prop="orAnd" /> -->
      <el-table-column label="条件" align="center" prop="conditionName" />
      <el-table-column label="表名" align="center" prop="tableName" />
      <el-table-column label="表描述" align="center" prop="tableComment" />
      <el-table-column label="字段名" align="center" prop="fieldName" />
      <el-table-column label="字段中文名" align="center" prop="fieldComment" />
      <el-table-column label="操作符" align="center" prop="operator" />
      <el-table-column label="比较值" align="center" prop="compareValue" />
      <el-table-column label="查询语句id" align="center" prop="sqlId" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['conf:BiSqlWhereDwd:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['conf:BiSqlWhereDwd:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改查询命令筛选条件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="函数名" prop="functionName">
          <el-select
            v-model="form.functionName"
            placeholder="请输入函数名"
            clearable
          >
            <el-option
              v-for="dict in dict.type.sys_sql_select_func"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="关键字名" prop="keywordName">
          <el-input v-model="form.keywordName" placeholder="请输入关键字名" />
        </el-form-item> -->
        <!-- <el-form-item label="优先级" prop="priorityLevel">
          <el-input
            style="width: 200px"
            v-model="form.priorityLevel"
            placeholder="请输入优先级"
          />
        </el-form-item>
        <el-form-item label="且或" prop="orAnd">
          <el-select
            v-model="form.orAnd"
            clearable
            filterable
            remote
            allow-create
          >
            <el-option
              v-for="item in orAndList"
              :key="item"
              :label="`${item}`"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="条件" prop="conditionName">
          <el-input
            style="width: 200px"
            v-model="form.conditionName"
            placeholder="请输入条件"
          />
        </el-form-item> -->
        <el-form-item label="表名" prop="tableName">
          <!-- <el-input v-model="form.tableName" placeholder="请输入表名" /> -->
          <el-select
            v-model="form.tableName"
            clearable
            filterable
            remote
            :remote-method="getDatacomment"
            placeholder="请输入表名"
            allow-create
            @change="changetableName"
          >
            <el-option
              v-for="item in tablecomment"
              :key="item.tableName"
              :label="`${item.comment}`"
              :value="item.tableName"
            >
            </el-option>
          </el-select>

          <el-select
            v-model="form.tableName"
            clearable
            filterable
            remote
            :remote-method="gettableName"
            placeholder="数据库表名"
            allow-create
            @change="changetableName"
          >
            <el-option
              v-for="item in tablecomment"
              :key="item.tableName"
              :label="`${item.tableName}`"
              :value="item.tableName"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="字段名" prop="fieldName">
          <!-- <el-input v-model="form.fieldName" placeholder="请输入字段名" /> -->
          <el-select
            v-model="form.fieldName"
            clearable
            filterable
            placeholder="请输入字段名"
            @focus="selectfieldNames(form.tableName)"
            @change="changefieldName"
          >
            <el-option
              v-for="item in fieldNames"
              :key="item.columnName"
              :label="`${item.comment}`"
              :value="item.columnName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作符" prop="operator">
          <!-- <el-input v-model="form.operator" placeholder="请输入操作符" /> -->
          <el-select v-model="form.operator" placeholder="请输入操作符">
            <el-option label="=" value="=" />
            <el-option label="!=" value="!=" />
            <el-option label=">" value=">" />
            <el-option label=">=" value=">=" />
            <el-option label="<" value="<" />
            <el-option label="<=" value="<=" />
            <el-option label="LIKE" value="LIKE" />
            <el-option label="BETWEEN" value="BETWEEN" />
            <el-option label="IS NOT NULL" value="IS NOT NULL" />
            <el-option label="IS NULL" value="IS NULL" />
          </el-select>
        </el-form-item>
        <el-form-item label="比较值" prop="compareValue">
          <el-select v-model="form.compareType" style="width: 120px">
            <el-option label="文本框" value="text" />
            <el-option label="日期控件" value="date" />
            <!-- <el-option label="今天" value="today" />
            <el-option label="过去天数" value="passdays" /> -->
          </el-select>
          <el-input
            v-show="form.compareType != 'date'"
            style="width: 200px"
            v-model="form.compareValue"
            placeholder="请输入比较值"
          />
          <el-date-picker
            v-show="form.compareType == 'date'"
            clearable
            v-model="dateValue"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期"
            @change="handleDate"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="查询语句id" prop="sqlId">
          <el-input v-model="form.sqlId" placeholder="请输入查询语句id" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiSqlWhereDwd,
  getBiSqlWhereDwd,
  delBiSqlWhereDwd,
  addBiSqlWhereDwd,
  updateBiSqlWhereDwd,
} from "@/api/conf/sql/BiSqlWhereDwd";
import {
  selectTableFields,
  selectTableNameList,
} from "@/api/conf/SysTableField";
import { listBiSqlFromTable } from "@/api/conf/sql/BiSqlFromTable";
export default {
  name: "BiSqlWhereDwd",
  dicts: ["sys_sql_select_func", "sys_sql_operator"],
  props: {
    id: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      dateValue: "",
      orAndList: ["or", "and"],
      fieldNames: [],
      tablecomment: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询命令筛选条件表格数据
      BiSqlWhereDwdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        functionName: null,
        keywordName: null,
        tableName: null,
        fieldName: null,
        operator: null,
        compareValue: null,
        sqlId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  watch: {
    id: {
      handler: function (newnew, oldold) {
        console.log("newnew", newnew);
        if (newnew) {
          this.queryParams.sqlId = newnew;
          // this.form.id=newnew

          this.getList();
          this.gettableNameList();
        }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    handleDate(key) {
      // console.log(JSON.stringify(key));
      // return;
      this.form.compareValue = this.dateValue;
      // this.openhandleDate = false;
    },
    changetableName(value) {
      const selectedOption = this.tablecomment.find(
        (option) => option.tableName === value
      );
      console.log(JSON.stringify(selectedOption)); // 这里就是整个选项对象
      this.form.tableComment = selectedOption.comment;
    },
    gettableNameList() {
      listBiSqlFromTable(this.queryParams).then((response) => {
        this.tablecomment = [];
        if (response.rows.length > 0) {
          response.rows.forEach((item) => {
            this.tablecomment.push({
              tableName: item.tableName,
              comment: item.tableComment ? item.tableComment : item.tableName,
            });
          });
        }
      });
    },
    changefieldName(value) {
      const selectedOption = this.fieldNames.find(
        (option) => option.columnName === value
      );
      console.log(JSON.stringify(selectedOption)); // 这里就是整个选项对象
      this.form.fieldComment = selectedOption.comment;
    },
    selectfieldNames(tableName) {
      if (!tableName) {
        return;
      }
      selectTableFields({ tableName: tableName }).then((response) => {
        this.fieldNames = response.map((item) => ({
          columnName: item.columnName,
          comment: item.comment ? item.comment : item.columnName,
        }));
      });
    },
    getDatacomment(key) {
      selectTableNameList({ comment: key }).then((response) => {
        this.tablecomment = response;
      });
    },
    gettableName(key) {
      selectTableNameList({ tableName: key }).then((response) => {
        this.tablecomment = response;
      });
    },
    /** 查询查询命令筛选条件列表 */
    getList() {
      this.loading = true;
      listBiSqlWhereDwd(this.queryParams).then((response) => {
        this.BiSqlWhereDwdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        // functionName: null,
        // keywordName: null,
        tableName: null,
        fieldName: null,
        operator: null,
        compareValue: null,
        sqlId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加查询命令筛选条件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiSqlWhereDwd(id).then((response) => {
        this.form = response.data;
        this.gettableName(this.form.tableName);
        this.open = true;
        this.title = "修改查询命令筛选条件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.sqlId = this.id;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiSqlWhereDwd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.dateValue = "";
            });
          } else {
            addBiSqlWhereDwd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.dateValue = "";
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除查询命令筛选条件编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiSqlWhereDwd(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "conf/BiSqlWhereDwd/export",
        {
          ...this.queryParams,
        },
        `BiSqlWhereDwd_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
